package asteroids;

public class Util {
  public static final double EPSILON = 0.0001;
  public static final double LARGER_EPSILON=0.1;

  public static boolean fuzzyEquals(double x, double y) {
    if (Double.isNaN(x) || Double.isNaN(y))
      return false;
    return Math.abs(x - y) <= EPSILON || Double.valueOf(x).equals(Double.valueOf(y));
  }

  public static boolean fuzzyLessThanOrEqualTo(double x, double y) {
    if (fuzzyEquals(x, y)) {
      return true;
    } else {
      return Double.compare(x, y) < EPSILON;
    }
  }
  
  public static double absoluteError(double expected, double actual) {
    return Math.abs(expected - actual);
  }

  public static double relativeError(double expected, double actual) {
    return absoluteError(expected, actual) / Math.abs(expected);
  }
  /**
   * Checks if a double is strictly less than another double.
   * 
   * @param 	x
   * 			The first double in x<y.
   * @param		y
   * 			The second double in x<y.
   * @return	True if and only if x is strictly less than y. 
   * 			| x<y
   */
  public static boolean fuzzyLessThan(double x, double y) {
	    if (!fuzzyEquals(x, y) && fuzzyLessThanOrEqualTo(x, y)) {
	      return true;
	    } 
	    else {
	      return false;
	    }
	  }

/**
 * Returns the 'sign' of the given double
 * @param x
 * 			The given double
 * @return 1 if x is positive or zero, -1 if it's negative.
 * 			| if(Util.fuzzyLessThan(x,0))
 *			| then result == -1
 *			| else result == 1
 *
 */
public static double getSign(double x) {
	if(Util.fuzzyLessThan(x,0)){
		return -1;
	}
	else{
		return 1;
	}
}
}